package com.zdkj.subway.check.controller.subwayRat.conductorDifferences.cheZhan;

import com.alibaba.fastjson.JSONObject;
import com.zdkj.common.core.util.R;
import com.zdkj.subway.check.entity.Page;
import com.zdkj.subway.check.entity.SwAuStDiffNotice;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.mapper.SwAuStDiffNoticeMapper;
import com.zdkj.subway.check.result.Result;
import com.zdkj.subway.check.service.SwAuStDiffNoticeService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 线网层级确认-车站票款差异-车站票款差异确认通知
 * 车站票款差异通知 SW_AU_ST_DIFF_NOTICE
 */
@Slf4j
@RestController
@RequestMapping("/checkNew")
public class SwAuStDiffNoticeCtrl {


    @Resource
    private SwAuStDiffNoticeService swAuStDiffNoticeService;
    @Resource
    private SwAuStDiffNoticeMapper swAuStDiffNoticeMapper;
    @Resource
    private SwAuStDiffNoticeController swAuStDiffNoticeController;

    /**
     * 车站票款差异通知 列表
     * @param param
     * @return
     */
    @RequestMapping("/getSwAuStDiffNoticeList")
    @ApiOperation(value = "车站票款差异通知 列表", notes = "车站票款差异通知 列表")
    public Page getSwAuStDiffNoticeList(Page page,@RequestParam Map<String,String> param){
        List<Map<String,Object>> swAuStDiffNoticeList =swAuStDiffNoticeMapper.getSwAuStDiffNoticeList(param);
        page.setData(swAuStDiffNoticeList,true);
        return page;
    }

    /**
     * 车站票款差异通知 单独查询
     * @param jsonObject
     * @return
     */
    @RequestMapping("/getSwAuStDiffNotice")
    @ApiOperation(value = "车站票款差异通知 单独查询", notes = "车站票款差异通知 单独查询")
    public Result getSwAuStDiffNotice(@RequestBody JSONObject jsonObject){
        return swAuStDiffNoticeService.getSwAuConductorNotice(jsonObject);
    }

    /**
     * 车站票款差异通知 申诉
     * @param jsonObject
     * @return
     */
    @RequestMapping("/update_xw_cz_pk_SwAuStDiffNotice")
    @ApiOperation(value = "车站票款差异通知 申诉", notes = "车站票款差异通知 申诉")
    public Result update_xw_cz_pk_SwAuStDiffNotice(@RequestBody JSONObject jsonObject){
        Result res=new Result(ResultDetailMsg.OK);
        try {
            SwAuStDiffNotice swAuStDiffNotice = JSONObject.parseObject(jsonObject.toJSONString(), SwAuStDiffNotice.class);
            int i = swAuStDiffNoticeMapper.updateById(swAuStDiffNotice);
        }catch (Exception e){
            log.error("{}",e);
            res.setMsg("提交异常");
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
        }
        return res;
    }

    /**
     * 车站票款差异通知 上传附件
     * @param
     * @return
     */
    @RequestMapping("/uploadFileCheZhanzhanNeiRYDLCK")
    @ApiOperation(value = "车站票款差异通知 上传附件", notes = "车站票款差异通知 上传附件")
    public Result uploadFileCheZhanzhanNeiRYDLCK(@RequestParam MultipartFile file) throws IOException {
        Result res=new Result(ResultDetailMsg.OK);
        try {
            String path="/data/minio/data/zdkj/"+file.getOriginalFilename();
            file.transferTo(new File(path));
        }catch (Exception e){
            log.error("{}",e);
            res.setMsg("上传异常");
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
        }
        return res;
    }

    /**
     * 车站票款差异通知 撤回申诉
     * @param
     * @return
     */
    @RequestMapping("/recallAppealSwAuStDiffNotice")
    @ApiOperation(value = "车站票款差异通知 撤回申诉", notes = "车站票款差异通知 撤回申诉")
    public Result recallAppealSwAuStDiffNotice(String ids) throws IOException {
        Result res=new Result(ResultDetailMsg.OK);
        try {
            List<SwAuStDiffNotice> swAuConductorNotices = swAuStDiffNoticeMapper.selectBatchIds(Arrays.asList(ids.split(",")));
            for(SwAuStDiffNotice swAuStDiffNotice:swAuConductorNotices){
                SwAuStDiffNotice s=new SwAuStDiffNotice();
                s.setId(swAuStDiffNotice.getId());
                //申诉状态 1：已申诉
                s.setAppealState("1");
                s.setFileUrl("");
                //审批状态 1：待审批
                s.setAuditSate("1");
                s.setAppealType("");
                s.setRemark("");
                s.setAppealReason("");
                s.setOvertimeReason("");
                swAuStDiffNoticeMapper.updateById(s);
            }
        }catch (Exception e){
            log.error("{}",e);
            res.setMsg("撤回异常");
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
        }
        return res;
    }

    /**
     * 车站票款差异通知 确认
     * @param
     * @return
     */
    @RequestMapping("/affirmSwAuConductorNotice")
    @ApiOperation(value = "车站票款差异通知 确认", notes = "车站票款差异通知 确认")
    public R affirmSwAuConductorNotice(Integer id){
        SwAuStDiffNotice swAuStDiffNotice = swAuStDiffNoticeMapper.selectById(id);
        Map<String,String> name = swAuStDiffNoticeMapper.selUser(swAuStDiffNotice.getAppealPersonCode());
        SwAuStDiffNotice s=new SwAuStDiffNotice();
        s.setId(id);
        s.setAppealState("6");
        s.setAuditSate("3");
        s.setApproverName(name.get("nikename"));
        s.setApproverJob(swAuStDiffNotice.getAppealPersonCode());
        s.setApproverTime(new Date());
        return R.ok(swAuStDiffNoticeController.approveDataSwAuStDiffNotice(String.valueOf(id),s));
    }


}
